package com.whl.mybatis.builder;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.whl.mybatis.constant.Constants;
import com.whl.mybatis.constant.SqlFormat;
import com.whl.mybatis.mapper.EntityTableMapper;
import com.whl.mybatis.mapper.PropertyColumnMapper;

public class SqlBuilder {
	public static String buildInsertSql(EntityTableMapper entityTableMapper, String mappedStatementSuffix) {
		boolean isSelective = mappedStatementSuffix.endsWith(StringUtils.capitalize(Constants.SELECTIVE));
		StringBuilder builder = new StringBuilder();
		builder.append(String.format(SqlFormat.INSERT_TABLE, entityTableMapper.getTableName()));
		List<PropertyColumnMapper> propertyColumnMappers = entityTableMapper.getPropertyColumnMappers();
		builder.append(String.format(SqlFormat.TRIM_PREFIX));
		for (PropertyColumnMapper propertyColumnMapper : propertyColumnMappers) {
			if (isSelective) {
				String format = String.format(SqlFormat.IF_TAG_COLUMN, propertyColumnMapper.getFieldName(),
						propertyColumnMapper.getColumnName());
				builder.append(format);
			} else {
				builder.append(propertyColumnMapper.getColumnName() + ",");
			}
		}
		builder.append(String.format(SqlFormat.TRIM_SUFFIX));
		builder.append(" VALUES ");
		builder.append(String.format(SqlFormat.TRIM_PREFIX));
		for (PropertyColumnMapper propertyColumnMapper : propertyColumnMappers) {
			if (isSelective) {
				builder.append(String.format(SqlFormat.IF_TAG_PROPERTY, propertyColumnMapper.getFieldName(),
						propertyColumnMapper.getFieldName()));
			} else {
				builder.append("#{" + propertyColumnMapper.getFieldName() + "},\n");
			}
		}
		builder.append(String.format(SqlFormat.TRIM_SUFFIX));
		System.out.println(builder);
		return builder.toString();
	}

	public static String buildUpdateSql(EntityTableMapper entityTableMapper, String mappedStatementSuffix) {
		boolean isSelective = mappedStatementSuffix.endsWith(StringUtils.capitalize(Constants.SELECTIVE));
		StringBuilder builder = new StringBuilder();
		builder.append(String.format(SqlFormat.UPDATE_TABLE, entityTableMapper.getTableName()));
		builder.append(String.format(SqlFormat.SET_PREFIX));
		List<PropertyColumnMapper> propertyColumnMappers = entityTableMapper.getPropertyColumnMappers();
		for (PropertyColumnMapper propertyColumnMapper : propertyColumnMappers) {
			if (isSelective) {
				builder.append(String.format(SqlFormat.IF_TAG_COLUMN_EQ_PROPERTY, propertyColumnMapper.getFieldName(),
						propertyColumnMapper.getColumnName(), propertyColumnMapper.getFieldName()));
			} else {
				builder.append(
						propertyColumnMapper.getColumnName() + "=#{" + propertyColumnMapper.getFieldName() + "},\n");
			}
		}
		builder.append(String.format(SqlFormat.SET_SUFFIX));
		builder.append(String.format(SqlFormat.WHERE_ONE_EQ_ONE));
		for (PropertyColumnMapper propertyColumnMapper : propertyColumnMappers) {
			if (propertyColumnMapper.getIsPrimaryKey()) {
				builder.append(
						String.format(SqlFormat.AND_IF_TAG_COLUMN_EQ_PROPERTY, propertyColumnMapper.getFieldName(),
								propertyColumnMapper.getColumnName(), propertyColumnMapper.getFieldName()));
				break;
			}
		}

		System.out.println(builder);
		return builder.toString();
	}

	public static String buildSelectSql(EntityTableMapper entityTableMapper, String mappedStatementSuffix) {
		boolean isByPrimaryKey = mappedStatementSuffix.endsWith(StringUtils.capitalize(Constants.BY_PRIMARY_KEY));
		boolean isSelective = mappedStatementSuffix.endsWith(StringUtils.capitalize(Constants.SELECTIVE));
		StringBuilder builder = new StringBuilder();
		builder.append(String.format(SqlFormat.SELECT_TABLE, entityTableMapper.getTableName()));
		builder.append(String.format(SqlFormat.WHERE_ONE_EQ_ONE));
		List<PropertyColumnMapper> propertyColumnMappers = entityTableMapper.getPropertyColumnMappers();
		for (PropertyColumnMapper propertyColumnMapper : propertyColumnMappers) {
			if (isByPrimaryKey) {
				if (propertyColumnMapper.getIsPrimaryKey()) {
					builder.append(" and "+ propertyColumnMapper.getColumnName() +"=#{0} ");
					break;
				}
			}
			if (isSelective) {
				builder.append(
						String.format(SqlFormat.AND_IF_TAG_COLUMN_EQ_PROPERTY, propertyColumnMapper.getFieldName(),
								propertyColumnMapper.getColumnName(), propertyColumnMapper.getFieldName()));
			} else {
				builder.append("and " + propertyColumnMapper.getColumnName() + "=#{"
						+ propertyColumnMapper.getFieldName() + "}\n");
			}
		}
		System.out.println(builder);
		return builder.toString();
	}

	public static String buildDeleteSql(EntityTableMapper entityTableMapper, String mappedStatementSuffix) {
		boolean byPrimaryKey = mappedStatementSuffix.endsWith(StringUtils.capitalize(Constants.BY_PRIMARY_KEY));
		boolean isSelective = mappedStatementSuffix.endsWith(StringUtils.capitalize(Constants.SELECTIVE));
		StringBuilder builder = new StringBuilder();
		builder.append(String.format(SqlFormat.DELETE_TABLE, entityTableMapper.getTableName()));
		builder.append(String.format(SqlFormat.WHERE_ONE_EQ_ONE));
		List<PropertyColumnMapper> propertyColumnMappers = entityTableMapper.getPropertyColumnMappers();
		for (PropertyColumnMapper propertyColumnMapper : propertyColumnMappers) {
			if (byPrimaryKey) {
				if (propertyColumnMapper.getIsPrimaryKey()) {
					builder.append(" and "+ propertyColumnMapper.getColumnName() +"=#{0} ");
					break;
				}
			}
			if (isSelective) {
				builder.append(
						String.format(SqlFormat.AND_IF_TAG_COLUMN_EQ_PROPERTY, propertyColumnMapper.getFieldName(),
								propertyColumnMapper.getColumnName(), propertyColumnMapper.getFieldName()));
			} else {
				builder.append("and " + propertyColumnMapper.getColumnName() + "=#{"
						+ propertyColumnMapper.getFieldName() + "}\n");
			}
		}
		System.out.println(builder);
		return builder.toString();
	}

}
